<?php
add_action( 'admin_enqueue_scripts', 'nino_contact_pro_enqueue');
function nino_contact_pro_enqueue($hook) {
	
	if (isset($_GET['page']) && $_GET['page'] == 'nino_contact_form_builder') {
		wp_enqueue_script('nino-contact-form-pro-ajax', NINO_CONTACT_PRO_URL . 'includes/assets/js/contact-pro-ajax.js', array('jquery','jquery-ui-core','jquery-ui-sortable', 'jquery-ui-accordion'));
		
		// in javascript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
		wp_localize_script( 'nino-contact-form-pro-ajax', 'nino_contact_pro_ajax_object',
	            array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
	}
}

add_action("wp_ajax_nino_save_form_builder", "nino_contact_pro_save_form");

function nino_contact_pro_save_form() {
	global $wpdb;
	
	$id = $_POST['id'];
	$name = $_POST['formName'];
	$contentMapJs = $_POST['contentMapJs'];
	$contentSettingJs = $_POST['contentSettingJs'];
	$mapContent = $_POST['mapContent'];
	$settingContent = $_POST['settingContent'];
	
	//Insert data
	$table = $wpdb->prefix."nino_contact_pro";
	$data = array(
		'id' => $id,
		'name' => $name,
		'contentMapJs' => $contentMapJs,
		'contentSettingJs' => $contentSettingJs,
		'mapContent' => json_encode($mapContent),
		'settingContent' => json_encode($settingContent),
	);
	
	$format = array(
		'%s',
		'%s',
		'%s',
		'%s',
		'%s',
		'%s'
	);
	
	$wpdb->insert( $table, $data, $format );
	
	die;
}